package com.metacube.questionbank.dao;

import java.util.List;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.metacube.questionbank.jdbc.TagRowMapper;
import com.metacube.questionbank.dto.*;

@Repository
public class TagDAO {

	@Autowired
	DataSource dataSource;
	
	public List<TagDTO> getAllTags() {
		JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
		String query="select t.*, count(q.tagId) as totalAnswers from questionTags q,tag t where q.tagId = t.tagId group by t.tagId";
		return (jdbcTemplate.query(query, new TagRowMapper()));
	}

	public int addTag(TagDTO tDTO) {
		JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
		String query = "insert into tag(tagName,description) value (?,?)";
		return jdbcTemplate.update(query, new Object[] { tDTO.getTagName(),tDTO.getDescription()});
		
	}
}
